package com.example.user.customviewdemo.slideletelistview.scroller;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * Created by user on 2016/5/24.
 */
public class MyScrollerLinearLayout extends LinearLayout {

    private Scroller mScroller;
    private int MAX_WIDTH = 300;//px

    public interface OnScrollListener{
        void onScroll(MyScrollerLinearLayout layout);
    }
    private OnScrollListener mOnScrollListener;

    public MyScrollerLinearLayout(Context context) {
        super(context);
    }

    public MyScrollerLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context,new LinearInterpolator());
    }

    public MyScrollerLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private float preX;
    private float preScrollX;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                preX = ev.getX();
                preScrollX = getScrollX();
                mScroller.startScroll(getScrollX(),getScrollY(),0,getScrollY());
                break;
            case MotionEvent.ACTION_MOVE:
                float x = ev.getX();
                float scrollX = preScrollX + preX - x;
                if(scrollX >= MAX_WIDTH){
                    scrollX = MAX_WIDTH;
                }else if(scrollX < 0){
                    scrollX = 0;
                }
                scrollTo((int) scrollX,0);
                preX = x;
                preScrollX = scrollX;
                break;
            case MotionEvent.ACTION_UP:
                //判断up时scrollX的大小，如果大于MAX_WIDTH/2则直接删除按钮展开，否则收缩
                float upScrollX = getScrollX();
                if(upScrollX > MAX_WIDTH/2){
                    mScroller.startScroll((int)upScrollX,0, (int) (MAX_WIDTH - upScrollX),0);
                    if(mOnScrollListener != null){
                        mOnScrollListener.onScroll(this);
                    }
                }else{
                    mScroller.startScroll((int)upScrollX,0, (int) -upScrollX,0);
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void computeScroll() {
        if(mScroller.computeScrollOffset()){
            this.scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
        }
        invalidate();
    }

    /**
     * 将控件scrollTo到指定位置
     * @param x
     * @param y
     */
    public void smoothScrollTo(int x,int y){
        int curreScrollX = getScrollX();
        int curreScrollY = getScrollY();
        mScroller.startScroll(curreScrollX,curreScrollY,x-curreScrollX,y-curreScrollY);
    }

    public void setOnScrollListener(OnScrollListener onScrollListener){
        this.mOnScrollListener = onScrollListener;
    }
}
